//index.js
//获取应用实例
var config = require('../../config/config.js');
var util = require('../../utils/util.js');
var http = require('../../utils/http.js');
var wxCharts = require('../../lib/wxcharts/wxcharts.js');
const app = getApp();
Page({
  data: {
    isLoading: false,
    overview: {},//场所概览信息
    form: {
      end_date: util.formatTime(new Date(), 'yyyy-MM-dd'),
      start_date: util.getBetweenTime('', -7)
    },
    nav: [{
        title: '新增用户',
        isActive: true
      },
      {
        title: '在线用户',
        isActive: false
      },
      {
        title: '在线设备',
        isActive: false
      },
      {
        title: '营运收入',
        isActive: false
      }
    ],//运营详情导航
    list: [],//用户列表
    navContent: {
      dateArr: [], //日期
      addUserNumArr: [], //新增用户数
      onlineUserMaxArr: [], //在线用户数
      onlineDeviceMaxArr: [], //在线设备数
      priceNumArr: [] //营运收入
    }, //运营详情信息

    // 图表显示配置
    echarts: {
      canvasId: 'lineCanvas',
      type: 'line',
      categories: ['0701', '0702', '0703', '0704', '0705', '0706', '0707'],
      series: [{
        name: '新增用户数量走势图',
        data: [1, 2, 4, 3, 4, 8, 11],
        format: function(val) {
          return val;
        }
      }],
      yAxis: {
        format: function(val) {
          return parseInt(val);
        },
        min: 0
      },
      width: 320,
      height: 200
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //验证微信是否授权
  getWeixinLogin(code) {
    let that = this;
    http.GET({
      url: 'api/publics/wxlogin', //仅为示例，并非真实的接口地址
      data: {
        "code": code
      },
      success: function(res) {
        //判断是否经过微信绑定，0为已经绑定微信号，0009为未绑定则跳转到绑定页面
        if (res.data.error === 0) {
          let obj = {
            isInit: true,
            start_date: util.getBetweenTime('', -7),
            end_date: util.formatTime(new Date(), 'yyyy-MM-dd')
          }
          wx.setStorageSync("sessionId", res.data.data);
          that.getOverview();
          that.getNavContent(obj);
          that.getUserInfo();
        } else if (res.data.error === '0009') {
          wx.setStorageSync('openId', res.data.data.open_id);
          wx.redirectTo({
            url: '../login/login',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
    this.setData({
      isLoading: true
    })
  },

  //获取用户信息
  getUserInfo(){
    let that = this;
    http.GET({
      url: 'api/user/getUserInfo', //仅为示例，并非真实的接口地址
      success: function (res) {
        if (res.data.error === 0) {
          wx.setStorageSync('user', res.data.data)
        }
      }
    })
  },
  
  //查看场所总体情况
  getOverview() {
    let that = this;
    that.setData({
      isLoading: true
    })
    that.isLoading = true;
    http.GET({
      url: 'api/Overview/overviewInfo', 
      success: function(res) {
        that.setData({
          isLoading: false
        })
        if (res.data.error === 0) {
          that.setData({
            overview: res.data.data
          })
        }
      }
    })
    this.setData({
      isLoading: true
    })
  },
  // 查看运营详情
  getNavContent(obj) {
    let that = this;
    this.setData({
      "form": obj
    });
    http.GET({
      url: 'api/Overview/datetimeDiff',
      data: that.data.form,
      success: function(res) {
        if (res.data.error === 0) {
          let data = res.data.data;
          that.setData({
            'list': data
          });
          let dateArr = [];
          let addUserNumArr = [];
          let onlineUserMaxArr = [];
          let onlineDeviceMaxArr = [];
          let priceNumArr = [];
          if (obj.hasOwnProperty('isInit')) {
            for (let index = data.length - 1; index > 0; index--) {
              dateArr.push(data[index].date);
              addUserNumArr.push(data[index].add_user_num);
              onlineUserMaxArr.push(data[index].online_user_max);
              onlineDeviceMaxArr.push(data[index].online_device_max);
              priceNumArr.push(data[index].price_num);
            }
          } 
          
          that.setData({
            "navContent.dateArr": dateArr
          });
          that.setData({
            "navContent.addUserNumArr": addUserNumArr
          });
          that.setData({
            "navContent.onlineUserMaxArr": onlineUserMaxArr
          });
          that.setData({
            "navContent.onlineDeviceMaxArr": onlineDeviceMaxArr
          });
          that.setData({
            "navContent.priceNumArr": priceNumArr
          });
          if (obj.hasOwnProperty('isInit')){
            that.initEcharts(dateArr, addUserNumArr, "新增用户数量走势图")
          }  
        }
      }
    })
  },
  // 根据点击不同的导航栏初始化不同的表格
  changeNav(e) {
    for (var index = 0; index < this.data.nav.length; index++) {
      var isActive = "nav[" + index + "].isActive";
      if (index == e.target.dataset.index) {
        this.setData({
          [isActive]: true
        });
      } else {
        this.setData({
          [isActive]: false
        })
      }
    }
    if (e.target.dataset.index == 0) {
      this.initEcharts(this.data.navContent.dateArr, this.data.navContent.addUserNumArr, "新增用户数量走势图");
    } else if (e.target.dataset.index == 1) {
      this.initEcharts(this.data.navContent.dateArr, this.data.navContent.onlineUserMaxArr, "在线用户数量走势图");
    } else if (e.target.dataset.index == 2) {
      this.initEcharts(this.data.navContent.dateArr, this.data.navContent.onlineDeviceMaxArr, "在线设备数量走势图");
    } else if (e.target.dataset.index == 3) {
      this.initEcharts(this.data.navContent.dateArr, this.data.navContent.priceNumArr, "营运收入走势图");
    }
  },
  // 初始化表格
  initEcharts(date, data, name) {
    this.setData({
      "echarts.categories": date
    });
    this.setData({
      "echarts.series[0].data": data
    });
    this.setData({
      "echarts.series[0].name": name
    });
    new wxCharts(this.data.echarts);
  },
  // 当日期改变的时候
  changeDate: function(e) {   
    this.getNavContent(e.detail);
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: '如来云平台',
      path: '/page/index/index'
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    // 登录
    wx.login({
      success: res => {
        that.getWeixinLogin(res.code);
      }
    })
  },
  onLoad: function() {
    
  }
})